package org.kumas.sgrpc.remoting.transport;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.kumas.sgrpc.common.URL;
import org.kumas.sgrpc.common.extension.ExtensionLoader;
import org.kumas.sgrpc.remoting.ChannelHandler;
import org.kumas.sgrpc.remoting.Codec;
import org.kumas.sgrpc.remoting.Endpoint;

/**
 *
 * @author KumasZhang
 * @version 1.0
 * @since 2021-07-20 10:01
 */
public abstract class AbstractEndpoint implements Endpoint, ChannelHandler {
    protected static final ConcurrentMap<String, String> CACHED_CODEC_NAME = new ConcurrentHashMap<>();

    static {
        CACHED_CODEC_NAME.putIfAbsent("telnet", "TelnetCodec");
        CACHED_CODEC_NAME.putIfAbsent("sgrpc", "SgrpcProtocol");
    }

    protected Codec getChannelCodec(URL url) {
        String protocol = url.getProtocol();
        String codecName = CACHED_CODEC_NAME.get(protocol);
        return ExtensionLoader.getExtensionLoader(Codec.class).getExtension(codecName);
    }
}
